<template>
  <el-card class="type-box">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="一级分类">
        <!-- option:label即为显示文字 value属性即为select下拉菜单收集的数据 -->
        <el-select
          placeholder="请选择"
          :disabled="props.scene == 0 ? false : true"
          clearable
          v-model="categoryStore.c1Id"
          @change="handler"
        >
          <el-option
            v-for="c1 in categoryStore.c1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          :disabled="props.scene == 0 ? false : true"
          clearable
          v-model="categoryStore.c2Id"
          @change="handlerTwo"
        >
          <el-option
            v-for="c2 in categoryStore.c2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择"
          :disabled="props.scene == 0 ? false : true"
          clearable
          v-model="categoryStore.c3Id"
        >
          <el-option
            v-for="c3 in categoryStore.c3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import {onMounted } from "vue";
// 引入分类相关的仓库
import useCategoryStore from "../../store/modules/category";

// 接收父组件传递过来的场景值
const props = defineProps({
  scene: {
    type: Number,
    default: 0,
  },
});

let categoryStore = useCategoryStore();

//  分类全局组件挂载完毕,通知仓库发请求获取一级分类的数据
onMounted(() => {
  getC1();
});
// 获取一级分类的数据
const getC1 = () => {
  // 通知分类仓库发请求获取一级分类的数据
  categoryStore.getCategory1();
};

// 一级分类选择值发生变化后,通知仓库发请求获取二级分类的数据(选中值的时候会触发,保证一级分类ID有了)
const handler = () => {
  categoryStore.c2Id = "";
  categoryStore.c3Id = "";
  categoryStore.c2List = [];
  categoryStore.c3List = [];
  // 通知仓库发请求获取二级分类的数据
  categoryStore.getCategory2();
};

const handlerTwo = () => {
  categoryStore.c3Id = "";
  categoryStore.c3List = [];
  // 通知仓库发请求获取二级分类的数据
  categoryStore.getCategory3();
};
</script>

<style scoped lang="scss">
.type-box {
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  overflow: hidden;
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
